# demo08_threading_test.py
#### 练习 1：扫描存活主机
# 1. 通过 ping 测试主机是否可达
# 2. 如果 ping 不通，不管什么原因都认为主机不可用
# 3. 通过多线程方式实现并发扫描
import subprocess, threading
def ping(host):  # 17：44 改成多线程测试
    # ping -c 2 192.168.1.10 &> /dev/null
    str01 = "ping -c 2 %s &> /dev/null" % host
    result = subprocess.run(str01, shell=True)
    if result.returncode == 0:
        print("%s: up" % host)
    else:
        print("%s: down" % host)
if __name__ == '__main__':
    host_list = [
        "www.baidu.com", "www.163.com", "www.qq.com",
        "www.tmooc.cn", "www.hao123.com", "www.4399.com",
        "www.baidu.com", "www.163.com", "www.qq.com",
        "www.tmooc.cn", "www.hao123.com", "www.4399.com"
    ]
    # 多线程
    for host in host_list:
        t = threading.Thread(target=ping, args=(host, ))
        t.start()
    # 单线程
    # for host in host_list:
    #     ping(host)

